<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师资管理</el-breadcrumb-item>
      <el-breadcrumb-item>收费管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="收费类型">
          <!--          <el-input v-model="queryInfo.typeId" clearable @clear="queryList" placeholder="请输入收费类型"></el-input>-->
          <template>
            <el-select v-model="queryInfo.typeId" clearable @clear="queryList" @change="queryList" filterable
              placeholder="请选择收费类型">
              <el-option v-for="item in chargeTypeList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="el-icon-plus" @click="toAddCharge">新增收费类型</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="chargeList" border style="width: 100%" :header-cell-style="{'text-align':'center'}"
      :cell-style="{'text-align':'center'}">
      <el-table-column width="80px" prop="id" label="ID"></el-table-column>
      <el-table-column prop="chargeType.name" label="收费类型">

      </el-table-column>
      <el-table-column prop="chargePrice" label="收费价格">
      </el-table-column>
      <el-table-column prop="countNumber" label="咨询人数">
      </el-table-column>
      <el-table-column prop="consult.name" label="咨询师">
        <template slot-scope="scope">
          <el-tag type="">{{ scope.row.consult.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" type="primary" size="mini" @click="toEditCharge(scope.row)">编辑</el-button>
          <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteCharge(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="charge">
        <el-form-item label="收费类型" label-width="80px">
          <template>
            <el-select v-model="charge.typeId" filterable placeholder="请选择">
              <el-option v-for="item in chargeTypeList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="收费价格" label-width="80px">
          <el-input placeholder="请输入收费价格" v-model="charge.chargePrice"></el-input>
        </el-form-item>
        <el-form-item label="咨询人数" label-width="80px">
          <el-input placeholder="请输入咨询人数" v-model="charge.countNumber"></el-input>
        </el-form-item>
        <el-form-item label="咨询师" label-width="80px">
          <template>
            <el-select v-model="charge.consultId" filterable placeholder="请选择">
              <el-option v-for="item in consultList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addCharge">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {url, url1} from "@/util/tools";

export default {
  data() {
    return {
// 查询信息对象
      queryInfo: {
        typeId:'',
        pageNum: 1,
        pageSize: 5
      },
      total: 183,
      chargeList: [],
      title:'',
      dialogFormVisible:false,
      charge:{
        "id":"",
        "typeId":'',
        "chargePrice":"",
        "countNumber":"",
        "consultId":''
      },
      chargeTypeList:[],
      consultList:[]
    }
  },
  methods: {
    // 删除文章
    deleteCharge(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
          .then(() => {
            this.$http.delete('charge/' + id)
                .then(resp => {
                  if (resp.data.status == 200) {
                    this.$message.success(resp.data.message)
                    this.queryList()
                  } else {
                    this.$message.error(resp.data.message)
                  }
                })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
    },


    //修改收费
    toEditCharge(row){
      this.charge=row;
      this.title="修改收费";
      this.dialogFormVisible=true;
    },


    //咨询类型查询
    queryChargeType(){
      this.$http.get('chargeType/findAll')
          .then(res=>{
            this.chargeTypeList=res.data.data;
          })
    },
//咨询师查询
    queryConsult(){
      this.$http.get('consult/findAll')
          .then(res=>{
            this.consultList=res.data.data;
          })
    },
cancle(){
  this.dialogFormVisible=false;
  this.charge = { // 文章对象
    "id":"",
    "typeId":'',
    "chargePrice":"",
    "countNumber":"",
    "consultId": ""
  }
},

    //添加文章
    addCharge(){
      if (this.charge.id){
        this.$http.put("charge/edit", this.charge)
            .then(resp => {
              if (resp.data.status == 200) {
                // 消息提示
                this.$message.success('修改成功！');
                // 清空数据
                this.charge = {
                  "id":"",
                  "typeId":"",
                  "chargePrice":"",
                  "countNumber":"",
                 "consultId":'',
                }
                // 关闭对话框
                this.dialogFormVisible = false;
                // 重新查询
                this.queryList()
                // this.queryChargeType()
              } else {
                this.$message.error('接口异常！')
              }
            })

      }else {
        this.$http.post("charge/add", this.charge)
            .then(resp=>{
              if (resp.data.status == 200) {
// 消息提示
                this.$message.success('添加成功！');
// 清空数据
                this.charge = { // 文章对象
                  "id":"",
                  "typeId":"",
                  "chargePrice":"",
                  "countNumber":"",
                 "consultId":""
                }
                this.dialogFormVisible=false;
                this.queryList()
                // this.queryChargeType()
              }
              else {
                this.$message.error("接口异常")
              }
            })
      }
    },
    toAddCharge(){
      this.title="新增收费";
      this.dialogFormVisible=true;
    },
    queryList() {
// 调用后端接口来获取数据
      this.$http.get('charge/findByPage', {
        params: this.queryInfo

      })
          .then(resp => {
            if (resp.data.status == 200) {
              // console.log(resp.data)
              this.chargeList = resp.data.data.list;
              this.total = resp.data.data.total;
//console.log(this.articleList)
            } else {
              this.$message.error('查询失败！')
            }
          })
    },

    handleSizeChange(size) {
      console.log('每页显示数量发生改变');
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
// 重新查询
      this.queryList()
    },
    handleCurrentChange(num) {
      console.log('页码发生改变');
      this.queryInfo.pageNum=num;
      this.queryList();
    }
  },
  mounted() {
    this.queryList()
    this.queryChargeType()
    this.queryConsult()
  }
}
</script>
<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}
.el-pagination {
  margin-top: 15px;
}
.el-link {
  padding: 0 5px;
}
</style>